<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>LayIM 3.x PC版本地演示</title>

    <link rel="stylesheet" href="../dist/css/layui.css">
    <style>
        html {
            background-color: #333;
        }
    </style>
</head>

<body>


    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="../dist/layui.js"></script>
    <script src="app.js"></script>

    <script>

        let iToolQueue = null;


        var Action = {
            //监听发送消息
            sendMessage: function (layim) {
                layim.on('sendMessage', function (data) {
                    var To = data.to;
                    var content = data.mine.content;
                    var type = 'text';

                    if (content.substring(0, 4) == "img[") {
                        type = 'img';
                        content = content.substring(4,content.length - 1);
                    } else if (content.substring(0, 5) == "file(") {
                        type = 'file';
                        content = content.substring(5,content.length - 7);
                    }

                    if (iToolQueue) {
                        iToolQueue.send({
                            to: To.id, // 对方的 sesstion id
                            type: type,
                            content: content
                        }, function (info) {
                            console.log(info)
                        });
                    }

                });
            }
        };



        layui.use('layim', function (layim) {

            // 用户身份信息
            var userInfo = {
                userName: "哈哈",
                headImg: "http://9echem.com/public/imgs/banner_binghua.png",
                age: 28
            };

            var config = {
                uuid: "123", // 用户身份标识
                projectToken: "6D7-BB91-13901823A235",
                parameter: JSON.stringify(userInfo)
            };

            iToolQueue = window.iToolQueue(config);

            iToolQueue.onopen(function () {
                console.log('onopen')
            });

            iToolQueue.onmessage(function (received_msg) {
                console.log(received_msg)
            });

            iToolQueue.onclose(function () {
                console.log('onclose')
            });

            iToolQueue.start();

            iToolQueue.login(function (code) {

                if (code == 200) {
                    layim.config({
                        title: 'Itool Chat',
                        init: {
                            mine: {
                                "username": userInfo.userName,
                                "id": config.uuid,
                                "status": "online",
                                "remark": "你好 我在。",
                                "avatar": userInfo.headImg
                            }
                        },

                        uploadImage: {
                            url: 'https://file.help-itool.com/layimupload/images'
                        },

                        uploadFile: {
                            url: 'https://file.help-itool.com/layimupload/file'
                        },

                        isfriend: false,
                        isgroup: false,
                        // brief: true,  // 客服模型
                        initSkin: '5.jpg',
                        min: true,
                        notice: true
                    });
                } else {
                    console.error('登录失败')
                }

            });

            iToolQueue.offline(function (received_msg) {
                console.log(received_msg)
            });

            iToolQueue.sesstion(function (received_msg) {

                var user = received_msg.userParameter;
                var params = received_msg.messageParameter;

                var message = {
                    id: received_msg.userKey,
                    type: "friend",
                    username: user.userName,
                    avatar: user.headImg,
                    content: received_msg.message
                };

                switch (params.type) {
                    case 'img':
                        message.content = 'img[' + received_msg.message + ']';
                        break;

                    case 'file':
                        message.content = 'file(' + received_msg.message + ')[下载文件]';
                        break;

                    case 'text':
                        break;
                    default: 
                        message.content = "[该消息类型不支持PC端打开]";
                        break;
                }

                layim.getMessage(message);



            });


            Action.sendMessage(layim);

        });


    </script>
</body>

</html>